﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Uway.EsheetServer.AutoCheckApp
{
    public partial class MainForm : Form
    {
        private ConfigModel _model = new ConfigModel();
        private bool _working = false;

        public MainForm()
        {
            InitializeComponent();
            bindingSource1.DataSource = _model;
            comboBox2.SelectedItem = "21点";
            comboBox1.SelectedItem = "9点";
        }

        public int BeginHour
        {
            get { return ParseHour(comboBox1.SelectedItem.ToString()); }
        }

        public int EndHour
        {
            get { return ParseHour(comboBox2.SelectedItem.ToString()); }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (_working)
            {
                MessageBox.Show("已开始");
                return;
            }

            if (EndHour < BeginHour)
            {
                MessageBox.Show("结束时间必须大于等于开始时间");
                return;
            }

            timer1.Interval = _model.Minutes * 60 * 1000;
            timer1.Start();

            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            comboBox2.Enabled = false;
            comboBox1.Enabled = false;
            _working = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();

            _working = false;
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            comboBox2.Enabled = true;
            comboBox1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {            
            var currentHour = DateTime.Now.Hour;
            // 非限定时间不发送短信
            if (currentHour < BeginHour || currentHour > EndHour)
            {
                return;
            }

            var msg = _model.SendMessage();

            if (!string.IsNullOrEmpty(msg))
            {
                var output = string.Format("{0} {1}\r\n", DateTime.Now, msg);

                if (textBox4.Text.Length >= textBox4.MaxLength)
                    textBox4.Text = string.Empty;

                textBox4.Text += output;
                UwaySoft.EsheetServer.BizLogic.Log.Info(output);
            }
        }

        private void menuClearOutput_Click(object sender, EventArgs e)
        {
            textBox4.Text = string.Empty;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_working)
            {
                MessageBox.Show("正在运行，无法关闭");
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
            base.OnClosing(e);
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            ShowInTaskbar = true;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                ShowInTaskbar = false;
            }
        }

        private int ParseHour(string hourText)
        {
            return int.Parse(hourText.Replace("点", string.Empty));
        }
    }
}
